
import { AudioClip, AudioSource, Component } from 'cc';
/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
export class SoundMgr extends Component {
    public static Instance: SoundMgr = null as unknown as SoundMgr;
    private static MAX_SOUNDS: number = 8; // 最大音效的数目

    private nowIndex: number = 0;
    private sounds: Array<AudioSource> = [];
    private bgMusic: AudioSource = null as unknown as AudioSource;

    private _isMusicMute: boolean = false;//是否静音
    
    public get isMusicMute() : boolean {
        return this._isMusicMute
    }
    
    public set isMusicMute(v : boolean) {
        this._isMusicMute = v;
        if(v){
            this. setMusicVolume(0)

        }
    }
    
    public Init(){}
    private _isSoundMute: boolean = false;

    public get isSoundMute() : boolean {
        return this._isSoundMute
    }
    
    public set isSoundMute(v : boolean) {
        this._isSoundMute = v;
        if(v){
            this. setSoundsVolume(0)
        }
       
    }

    private musicVolume: number = 1;

    private soundVolume: number = 1;

    onLoad(): void {
        if (SoundMgr.Instance === null) {
            SoundMgr.Instance = this;
        }
        else {
            this.destroy();
            return;
        }

        for (let i = 0; i < SoundMgr.MAX_SOUNDS; i++) {
            var as = this.node.addComponent(AudioSource);
            this.sounds.push(as);
        }

        this.bgMusic = this.node.addComponent(AudioSource) as AudioSource;
    }
     
    public playBgMusic(clip: AudioClip, isLoop: boolean): void {
        if(this._isMusicMute===true){
            return
        }
       let local_musicVolume = localStorage.getItem("linghuan_MusicVolume");

       
        if (local_musicVolume) {
            this.musicVolume = Number(local_musicVolume);
        }
        this.bgMusic.clip = clip;
        this.bgMusic.loop = isLoop;
        this.bgMusic.volume = this.musicVolume;

      //  console.log("-----this.bgMusic.volume", this.bgMusic.volume);

        this.bgMusic.play();
    }

    public stopBgMusic(): void {
        this.bgMusic.stop();
    }


    public playSound(clip: AudioClip,volume?:number): void {

        if (this._isSoundMute === true) {
            return;
        }
        let local_soundVolume = localStorage.getItem("linghuan_SoundsVolume");
      //  console.log(local_soundVolume);
        
        if (local_soundVolume) {
            this.soundVolume = Number(local_soundVolume);
        }

        var as = this.sounds[this.nowIndex];
        this.nowIndex++;
        if (this.nowIndex >= SoundMgr.MAX_SOUNDS) {
            this.nowIndex = 0;
        }

        as.clip = clip;
        as.loop = false;
        if(volume){
            as.volume=volume
        }else{
            as.volume = this.soundVolume;
        }
       
        as.play();
    }

    public playSoundOneShot(clip: AudioClip,volume?:number): void {
        if (this._isSoundMute === true) {
            return;
        }
        let local_soundVolume = localStorage.getItem("linghuan_SoundsVolume");
        console.log(local_soundVolume);
        
        if (local_soundVolume) {
            this.soundVolume = Number(local_soundVolume);
        }



        var as = this.sounds[this.nowIndex];
        this.nowIndex++;
        if (this.nowIndex >= SoundMgr.MAX_SOUNDS) {
            this.nowIndex = 0;
        }

        as.clip = clip;
        as.loop = false;
          if(volume){
            as.volume=volume
        }else{
            as.volume = this.soundVolume;
        }
        as.playOneShot(clip,volume);
    }

    // 控制音乐音量
    public setMusicVolume(value: number): void {
        this.bgMusic.volume = value;
       localStorage.setItem("linghuan_MusicVolume", value.toString());
    }
    // public setOneVolume(v:AudioSource,s:number){
       
    //         v.volume = s;
        
    // }
    // 控制音效音量
    public setSoundsVolume(value: number): void {
        for (let i = 0; i < this.sounds.length; i++) {
            this.sounds[i].volume = value;
        }

      

        localStorage.setItem("linghuan_SoundsVolume", value.toString());
    }
}

